package com.example.rmation.service.impl;


import com.example.rmation.common.MyExecption;
import com.example.rmation.mapper.UserMapper;
import com.example.rmation.model.entity.User;
import com.example.rmation.model.request.AddUserRequest;
import com.example.rmation.model.request.UpdateUserRequest;
import com.example.rmation.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public User login(String account, String password) {
        User user = userMapper.login(account,password);
        if (user == null) {
            throw new MyExecption(500,"账号密码错误");
        }
        return user;
    }

    @Override
    public PageInfo studentAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> studentList = userMapper.studentAll();
        PageInfo pageInfo = new PageInfo(studentList);
        return pageInfo;
    }

    @Override
    public void insertUser(AddUserRequest addUserRequest) {
        User user = new User();
        BeanUtils.copyProperties(addUserRequest,user);
        //查询账号是否已经存在
        String account = addUserRequest.getAccount();
        User userOld = userMapper.userByAccount(account);
        if (userOld != null) {
            throw new MyExecption(5000,"账号已存在!");
        }
        //效验通过后进行的业务
        int addCount = userMapper.insertSelective(user);
        if (addCount == 0) {
            throw new MyExecption(5000,"注册失败!");
        }
    }

    @Override
    public void outOfService(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        if (user.getAccountStatus() == 0) {
            throw new MyExecption(5000,"该账号已经被停用");
        }
        int updateCount = userMapper.updateByAccountStatus(id);
        if (updateCount == 0) {
            throw new MyExecption(5000,"停用失败!");
        }
    }

    @Override
    public void UpdateUser(UpdateUserRequest updateUserRequest) {
        User user = new User();
        BeanUtils.copyProperties(updateUserRequest,user);
        //查询账号是否已经存在
        String account = updateUserRequest.getAccount();
        User userOld = userMapper.userByAccount(account);
        if (userOld != null && !userOld.getId().equals(updateUserRequest.getId())) {
            throw new MyExecption(5000,"账号已存在,更新失败!");
        }
        //效验通过后进行的业务
        int updateCount = userMapper.updateByPrimaryKeySelective(user);
        if (updateCount == 0) {
            throw new MyExecption(5000,"更新失败!");
        }
    }

    @Override
    public PageInfo studentLike(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> studentList = userMapper.studentLike(name);
        PageInfo pageInfo = new PageInfo(studentList);
        return pageInfo;
    }
}
